﻿package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	public class AnimatedSprite extends Bitmap
	{
		
		public var Animations:Array = new Array();

		private var texture:String;
		

			
		private var currentAnimation:String = null;

		private var updateAnimation:Boolean = true;
		
		/**
		 * Timer used for frame animation.
		 * @private
		 */
		private var _timer:FrameTimer;
		
		/**
		 * Point object used for copyPixels operation.
		 * @private
		 */
		private var _point:Point;

		
		public function AnimatedSprite() 
		{
			
			super(new BitmapData(1, 1, true, 0x00000000),"auto",false); 
			this.smoothing = true;
			_point = new Point(0, 0);
			
		}

		
		public function get Texture():String
		{
			return texture
		}
		public function set Texture(value:String):void
		{
			texture = value;
		}
		
		public function get CurrentAnimation():String
		{	
			return currentAnimation; 
		}
		public function	set CurrentAnimation(value:String):void
		{
			
			if (Animations[value] == null)
				throw new Error("Invalid animation specified.");

			if (currentAnimation == null || currentAnimation != value)
			{
				currentAnimation = value;
				Animations[currentAnimation].Reset();
			}
		}
		
		public function StartAnimation():void
		{
			updateAnimation = true;
		}

		public function StopAnimation():void
		{
			updateAnimation = false;
		}

		public function Update():void
		{
			
				if (updateAnimation)
				{
					Animations[currentAnimation].Update();
					Draw();
					if (Animation(Animations[currentAnimation]).bDead)
					{
						if (currentAnimation == 'man_death')
						{
							this.StopAnimation();
							World.bEndMenu = true;
						}
						else
						{
							gameObject(this.parent).die();
						}
					}

						
					
				}
		}
		
		private function Draw():void
		{			
			bitmapData = GlobalAnimations.frames[this.currentAnimation][Animation(Animations[currentAnimation]).CurrentFrame];
		}
		
		public function Dispose():void
		{
			this.bitmapData = null;
			this.Animations = null;
			this.currentAnimation = null;
			this.texture = null;
		}

	}
	
}